package com.suray.rgv.son.mother.core;

import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.utils.LoggerType;
import com.suray.rgv.son.mother.util.RgvSMLogger;
import com.suray.rgv60.core.Rgv60Info;
import com.suray.rgv60.util.RobotIOType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 子母车数据
 */
public class RgvSMInfo {
    protected String series;

    protected Rgv60Info rgv60Info;

    protected List<Node> mapNodes;

    /**
     * 光电是否检测到母车内子车到位
     */
    protected boolean hasSonRgv;

    /**
     * 子车检测 - 左光电
     */
    protected Boolean leftRgvSignal;

    /**
     * 子车检测 - 右光电
     */
    protected Boolean rightRgvSignal;

    public RgvSMInfo(String serialNum, Rgv60Info rgv60Info) {
        this.series = serialNum;
        this.rgv60Info = rgv60Info;
        this.hasSonRgv = false;

        this.mapNodes = new ArrayList<>();
    }

    /**
     * 解析IO状态
     * 0左，1右
     * 两个光电都照到才算子车在母车内
     * 信号需要取反
     */
    public void analyzeRobotIO() {
        Map<RobotIOType.ROBOT_DI_TYPE, Boolean> DIs = rgv60Info.getDIs();
        Map<RobotIOType.ROBOT_DO_TYPE, Boolean> DOs = rgv60Info.getDOs();

        try {
            leftRgvSignal = DIs.get(RobotIOType.ROBOT_DI_TYPE.DI_0);
            rightRgvSignal = DIs.get(RobotIOType.ROBOT_DI_TYPE.DI_1);
        } catch (Exception e) {
            RgvSMLogger.exceptionLog(LoggerType.ERROR, series, "解析IO信号", e, this.getClass());
        }

        if (leftRgvSignal != null && rightRgvSignal != null) {
            //信号需要取反
            if (!leftRgvSignal && !rightRgvSignal) {
                hasSonRgv = true;
            } else {
                hasSonRgv = false;
            }
        } else {
            hasSonRgv = false;
        }
    }
}
